package com.company;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class test1 {
    public static void main(String[] args){
        try {
            test1.margeImage("E:\\image\\IDPhoto\\11.jpg", "E:\\image\\IDPhoto\\12.jpg", "E:\\image\\IDPhoto\\15.jpg");
        }catch (Exception e){

        }
    }
    public static void margeImage(String path1,String path2,String outPath) throws IOException {
        File file = new File(path1);
        Image img = ImageIO.read(file);
//水印宽度
        int width = img.getWidth(null);
//水印高
        int height = img.getHeight(null);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        ImageIcon imgIcon = new ImageIcon(path2);
//宽
        int imgIconWidth = imgIcon.getIconWidth();
//高
        int imgIconHeight = imgIcon.getIconHeight();
        Image con = imgIcon.getImage();
//透明度
        float clarity = 1.0f;
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, clarity));
//水印的位置
        g.drawImage(con, (width-imgIconWidth)/4, (height-imgIconHeight)/4, null);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        g.dispose();
        File sf = new File(outPath);
// 保存图片
        ImageIO.write(bi, "jpg", sf);


    }
}
